/* -----------------------------------------------------------------------
 * Becky Kluth
 * CS 362
 * Assignment 4: randomtestadventurer.c to test adventurer card with random inputs
 *
 * randomtestadventurer: randomtestadventurer.c dominion.o rngs.o
 *      gcc -o randomtestadventurer -g  randomtestadventurer.c dominion.o rngs.o $(CFLAGS)
 * -----------------------------------------------------------------------
 */

#include "dominion.h"
#include "dominion_helpers.h"
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include "rngs.h"
#include <time.h>
#include <stdlib.h>

#define NOISY_TEST 0										//set NOISY_TEST to 1 to print passing tests as well as failing tests
#define NUMBER_TESTS 10000									//macro to set total number of tests	
#define MAX_CARDS 100										//macro to set max number of cards in player 0's hand + deck + discard

void testAdventurer();

int main(){
	int i;
	printf("Testing adventurer card\n");

	for (i = 0; i < NUMBER_TESTS; i++){						//test adventurer NUM_TESTS times
		testAdventurer();
	}

	return 0;
}	

void testAdventurer(){
	int seed = 1000;										
    int numPlayer;				
    int k[10] = {adventurer, council_room, feast, gardens, mine, remodel, smithy, village, baron, great_hall};
    struct gameState G;
	int startingHandCount, startingDeck, startingDiscard, deckGold, discardGold;
	int noCrash;
	int i;

	numPlayer = (rand() % 3) + 2;							//randomly select 2 to 4 players
#if (NOISY_TEST == 1)
     printf("%d players.\n", numPlayer);
#endif

    initializeGame(numPlayer, k, seed, &G);					//initialize new game

	G.handCount[0] = rand() % (MAX_CARDS);					//randomly assign 0 to MAX_CARDS-1 cards in starting hand
	startingHandCount = G.handCount[0];	
#if (NOISY_TEST == 1)
     printf("%d hand.\n", startingHandCount);
#endif
	for(i = 0; i < G.handCount[0]; i++){					//fill hand with estate
		G.hand[0][i] = estate;
	}

	G.deckCount[0] = rand() % (MAX_CARDS - startingHandCount);	//assign random number of remaining cards to deck
	startingDeck = G.deckCount[0];	
#if (NOISY_TEST == 1)
     printf("%d deck.\n", startingDeck);
#endif
	for(i = 0; i < G.deckCount[0]; i++){					//fill deck with estate
		G.deck[0][i] = estate;
	}
	if(startingDeck >= 3){
		deckGold = rand() % (4);							//randomly add 0 to 3 gold to deck
		for(i = 0; i < deckGold; i++){					
			G.deck[0][i] = gold;
		}
	} else {
		deckGold = 0;
	}
#if (NOISY_TEST == 1)
     printf("%d deck gold.\n", deckGold);
#endif


	G.discardCount[0] = rand() % (MAX_CARDS - (startingHandCount + startingDeck));	//assign random number of remaining cards to played
	startingDiscard = G.discardCount[0];
#if (NOISY_TEST == 1)
     printf("%d discard.\n", startingDiscard);
#endif
	for(i = 0; i < G.discardCount[0]; i++){					//fill played with estate
		G.discard[0][i] = estate;
	}
	if(startingDiscard >= 3){
		discardGold = rand() % (4);							//randomly add 0 to 3 gold to discard
		for(i = 0; i < discardGold; i++){					
			G.discard[0][i] = gold;
		}
	} else {
		discardGold = 0;
	}
#if (NOISY_TEST == 1)
     printf("%d discard gold.\n", discardGold);
#endif

#if (NOISY_TEST == 1)
     printf("Test player 0 starts with %d cards in hand, %d in deck, %d in discard, %d gold in deck, %d gold in discard.\n", 
		 startingHandCount, startingDeck, startingDiscard, deckGold, discardGold);
#endif

	noCrash = cardEffect(adventurer, 0, 0, 0, &G, 1, 0);		//play adventurer card, should +2 cards to hand if treasure in deck + discard >= 2, -1 from hand for overall +1 to hand
	if(noCrash != 0){
		printf("Crash!\n");
	}

	if((deckGold + discardGold >= 2) && (G.handCount[0] != startingHandCount + 1)){				//failed to add 2 treasure and discard 1 when >=2 treasure in deck + discard
		printf("FAIL: %d players. Start: %d hand, %d deck, %d discard, %d gold in deck, %d gold in discard. End: %d hand\n", numPlayer, startingHandCount, startingDeck, startingDiscard, 
			deckGold, discardGold, G.handCount[0]);
	} else if ((deckGold + discardGold == 1) && (G.handCount[0] != startingHandCount)){			//failed to add 1 treasure and discard 1 when 1 treasure in deck + discard
		printf("FAIL: %d players. Start: %d hand, %d deck, %d discard, %d gold in deck, %d gold in discard. End: %d hand\n", numPlayer, startingHandCount, startingDeck, startingDiscard, 
			deckGold, discardGold, G.handCount[0]);
	} else if ((deckGold + discardGold == 0) && (G.handCount[0] != startingHandCount - 1)){		//failed to add 0 treasure and discard 1 when 0 treasure in deck + discard
		printf("FAIL: %d players. Start: %d hand, %d deck, %d discard, %d gold in deck, %d gold in discard. End: %d hand\n", numPlayer, startingHandCount, startingDeck, startingDiscard, 
			deckGold, discardGold, G.handCount[0]);
	} else {																					//passed!
#if (NOISY_TEST == 1)
		 printf("PASS: Test player 0 ends with %d cards in hand when %d gold in deck and %d gold in discard.\n", G.handCount[0], deckGold, discardGold);
#endif
	}

}